<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/26
 * Time: 14:48
 */

namespace App\Http\Controllers\API\LifeCycle\Package;

use App\Http\Controllers\Controller;
use App\Http\Resources\PackageUsedLogCollection;
use App\Http\Resources\PackageUsedLogDetailResource;
use App\Http\Resources\PackageUsedLogResource;
use App\Models\CarPackageUsedLog;
use App\Services\LifeCycle\UsedService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UsedController extends Controller
{
    protected $model = CarPackageUsedLog::class;
    protected $collection = PackageUsedLogCollection::class;
    protected $resource = PackageUsedLogResource::class;

    /**
     * @param Request $request
     */
    public function usedList(Request $request)
    {
        $param = $request->all();
        $user = Auth::user();
        $where = [];

        $keywords = $param['keywords'] ?? '';
        $order_id = $param['order_id'] ?? '';

        if ($keywords != '') {
            $where[] = ['attr_title', 'like', "%{$keywords}%"];
        }

        if ($order_id != '') {
            $where['order_id'] = $order_id;
        }

        $orm = $this->model::where($where)->orderBy('id', 'desc');

        $per_page = 10;

        $list = $orm->paginate($per_page);

        $data = new $this->collection($list);

        return $this->success($data);
    }

    public function detail(Request $request)
    {
        $used = new PackageUsedLogDetailResource(UsedService::detail($request->get('used_id')));

        return $this->success($used);
    }
}